#include "work.h"
#include "peripheral.h"
#include "timer.h"
#include "lpuart.h"
#include "flash.h"
#include "temp.h"
#include "basic.h"
#include "lpm.h"
#include "atim3.h"
#include "my_check_sleep.h"


extern uint8_t bat_low_flag;
extern uint8_t Beep_Time;
extern boolean_t age_mode,life_mode;
extern boolean_t load_f;
extern uint8_t Work_Mode;
extern boolean_t Debug_Mode;
extern uint8_t Charge_State;


static uint8_t off_cnt = 0;
uint8_t off_cnt2 = 0;
uint16_t off_cnt3, off_cnt1, sleep_cnt = 0;
uint16_t err_to_sleep_cnt = 0;


// uint8_t charge_delay2 = 0, charge_delay1 = 0, charge_delay = 0;
// uint16_t  chage_full_delay, charge_full_flag;


/**************************************************************
 * 
 *  进入睡眠/低功耗， 关机执行 10秒之后，整体关闭gpio
********************************************************/
void Halt_Handler(void)
{
	
	if(bat_low_flag){
		err_to_sleep_cnt ++;
		if(err_to_sleep_cnt == 250){
			Beep_Time = 3; 
		}
		if(err_to_sleep_cnt == 300){
			Work_Mode = 0;
			bat_low_flag = 0;
			// EN_VBAT_OFF; // 掉电，整体关机
		}
	}

	if(Work_Mode==0&&Charge_State==0&&K2&&USB_IN==0)	//���߹���2ua
	{
		sleep_cnt++;
		if(sleep_cnt > 200){
			EN_VBAT_OFF; // 掉电，整体关机  2秒后再掉电
		}
		if(sleep_cnt>3500)
		{
			
			sleep_cnt=0;
			Debug_Mode=0;
#if 0	
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_ADC);
			ADC->CR0&=0xfffffffe;
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_LPUART0);
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_ATIM3);
			ATIM3_Mode23_Stop();
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_GTIM);
			Btim_Disable(BTIM3);
			Btim_DisableIT(BTIM3, BTIM_IT_UI);         
			EnableNvic(BTIM3_4_5_IRQn, IrqLevel0, FALSE);
			
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_PA);
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_PB);
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_PC);
			SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_PD);
			
				///< ����
			REG_WRITE(GPIOA->ADS, 0x00U);
			REG_WRITE(GPIOB->ADS, 0x00U);
			REG_WRITE(GPIOC->ADS, 0x00U);
			REG_WRITE(GPIOD->ADS, 0x00U);
				
			REG_SETBITS(GPIOA->OUT, 0x00); 
			REG_SETBITS(GPIOB->OUT, 0x00); 
			REG_SETBITS(GPIOC->OUT, 0x00); 
			REG_SETBITS(GPIOD->OUT, 0x00); 
					
			///< ����
			REG_WRITE(GPIOA->DIR, 0xFFU);
			REG_WRITE(GPIOB->DIR, 0xEFU);
			REG_WRITE(GPIOC->DIR, 0xFFU);
			REG_WRITE(GPIOD->DIR, 0xFFU);
				
			REG_WRITE(GPIOA->PU, 0xe8);
			REG_WRITE(GPIOB->PU, 0xc0);
			REG_WRITE(GPIOC->PU, 0xe7);
			REG_WRITE(GPIOD->PU, 0x83);
					
			SYSCTRL_FuncEnable(SYSCTRL_FUNC_SWDIO);
		
			LPM_GotoDeepSleep(FALSE);	//�жϻ��Ѻ��Զ�����
			
			User_Init();
			App_Btimer_Init();
			App_LpUartPortCfg();
			System_ADCInit();
			System_PWMInit();
#endif			
		}
	}
	else{
		sleep_cnt = 0;
	}
}

/*****************************
 * 工作计时函数
 * 10ms 任务
 *****************************/
void Work_CountTime(void)	
{
	uint32_t AD_Result;

	off_cnt++;
	if(off_cnt > 99){
		off_cnt = 0;
		if(Work_Mode)
		{
			if((load_f == 1) || (off_cnt1 > 2)){
				off_cnt1++;
			}	//从带载开始计时
			
			if(off_cnt1 > 60*20+5){
				off_cnt1 = 0;
				Work_Mode = 0;
				Beep_Time = 3;
			}
			if(load_f == 0){
				off_cnt2++;
				if(off_cnt2 > 59){
					off_cnt2 = 0;
					Work_Mode = 0;
					Beep_Time = 3;
				}
			}
			else{
				off_cnt2 = 0;
			}

			if((age_mode == 1)||(life_mode == 1)){
				off_cnt1=0;
				if(life_mode){
					off_cnt3++;
					if(off_cnt3 > 600){
						off_cnt3 = 0;
					}
				}
				if(age_mode){

				}
			}
		}
		else{
			off_cnt3 = 0;
			off_cnt2 = 0;
			off_cnt1 = 0;
		}
	}
}

